import axios from '@/libs/api.request'

/**
 * 登录
 * @param loginName
 * @param password
 * @returns {http.ClientRequest | ClientHttp2Stream | * | never | AxiosPromise<any>}
 */
/*export const login = (loginName, password) => {
  const data = {
    loginName,
    password
  }
  return axios.request({
    url: 'auth/login',
    method: 'post',
    dataType: 'json',
    headers: {
      'Content-Type': 'application/json; charset=UTF-8'
    },
    data: data
  })
}*/

/** */
/**
 * 后台用Spring security 所以此处必须用POST
 * 如用get则会在后台跳到login.html页面
 * @param loginName
 * @param password
 * @param sessionId
 * @param verifyCode
 * @returns {*}
 */
export const login = (loginName, password,sessionId,verifyCode) => {
  let url='login?username='+loginName + "&password=" + encodeURIComponent(password) + "&sessionId="+sessionId+"&verifyCode=" + verifyCode;
  if (process.env.NODE_ENV !== "production"){
    url = url + "&dev=1"
  }
  //console.log(url);
  //console.log(encodeURIComponent(url));
  return axios.request({
    url: url,
    method: 'post',
    dataType: 'json',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8'
    }
  })
}

/**
 * 验证登录
 * @param loginName
 * @param password
 * @returns {AxiosPromise}
 */
export const checkLogin = (loginName, password) => {
  let url='login?username='+loginName + "&password=" + password + "&sessionId=1&verifyCode=1&dev=1";
  return axios.request({
    url: url,
    method: 'post',
    dataType: 'json',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8'
    }
  })
}

/**
 * 退出登录
 * @param token
 * @returns {http.ClientRequest | ClientHttp2Stream | * | never | AxiosPromise<any>}
 */
export const logout = (token) => {
  return axios.request({
    url: '/auth/logout?token=' + token,
    method: 'get',
    dataType: 'json',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8',
      'token': token
    },
  })
}
/**
 * 登录用户切换登录单位
 * @param token
 * @param orgCode 单位编码
 * @returns {AxiosPromise}
 */
export const loginChangeOrg = (token,orgCode) => {
  return axios.request({
    url: '/auth/login/chanage/org?orgCode='+orgCode,
    method: 'post',
    dataType: 'json',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8',
      'token': token
    },
  })
}

/**
 * 取得验证码
 * @returns {*}
 */
export const getVerifyCode = () => {
  return axios.request({
    url: '/verify/code/get',
    method: 'get',
    dataType: 'json',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8',
    },
  })
}



